namespace Meters
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using Utils;

    public class LBDefaultAnalogMeterRenderer : LBAnalogMeterRenderer
    {
        public override bool DrawBackground(Graphics gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            Color backColor = base.AnalogMeter.Parent.BackColor;
            SolidBrush brush = new SolidBrush(backColor);
            Pen pen = new Pen(backColor);
            Rectangle rect = new Rectangle(0, 0, base.AnalogMeter.Width, base.AnalogMeter.Height);
            gr.DrawRectangle(pen, rect);
            gr.FillRectangle(brush, rc);
            brush.Dispose();
            pen.Dispose();
            return true;
        }

        public override bool DrawBody(Graphics Gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            Color bodyColor = base.AnalogMeter.BodyColor;
            Color color2 = LBColorManager.StepColor(bodyColor, 20);
            LinearGradientBrush brush = new LinearGradientBrush(rc, bodyColor, color2, 45f);
            Gr.FillEllipse(brush, rc);
            float drawRatio = base.AnalogMeter.GetDrawRatio();
            RectangleF rect = rc;
            rect.X += 3f * drawRatio;
            rect.Y += 3f * drawRatio;
            rect.Width -= 6f * drawRatio;
            rect.Height -= 6f * drawRatio;
            LinearGradientBrush brush2 = new LinearGradientBrush(rect, color2, bodyColor, 45f);
            Gr.FillEllipse(brush2, rect);
            brush.Dispose();
            brush2.Dispose();
            return true;
        }

        public override bool DrawDivisions(Graphics Gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            PointF needleCenter = base.AnalogMeter.GetNeedleCenter();
            float startAngle = base.AnalogMeter.GetStartAngle();
            float endAngle = base.AnalogMeter.GetEndAngle();
            float scaleDivisions = base.AnalogMeter.ScaleDivisions;
            float scaleSubDivisions = base.AnalogMeter.ScaleSubDivisions;
            float drawRatio = base.AnalogMeter.GetDrawRatio();
            double minValue = base.AnalogMeter.MinValue;
            double maxValue = base.AnalogMeter.MaxValue;
            Color scaleColor = base.AnalogMeter.ScaleColor;
            float x = needleCenter.X;
            float y = needleCenter.Y;
            float width = rc.Width;
            float height = rc.Height;
            float radian = LBMath.GetRadian((endAngle - startAngle) / ((scaleDivisions - 1f) * (scaleSubDivisions + 1f)));
            float num13 = LBMath.GetRadian(startAngle);
            float num14 = (width / 2f) - ((float) (width * 0.08));
            float num15 = (float) minValue;
            Pen pen = new Pen(scaleColor, 2f * drawRatio);
            SolidBrush brush = new SolidBrush(scaleColor);
            PointF tf2 = new PointF(0f, 0f);
            PointF tf3 = new PointF(0f, 0f);
            for (int i = 0; i < scaleDivisions; i++)
            {
                tf2.X = x + ((float) (num14 * Math.Cos((double) num13)));
                tf2.Y = y + ((float) (num14 * Math.Sin((double) num13)));
                tf3.X = x + ((float) ((num14 - (width / 20f)) * Math.Cos((double) num13)));
                tf3.Y = y + ((float) ((num14 - (width / 20f)) * Math.Sin((double) num13)));
                Gr.DrawLine(pen, tf2, tf3);
                Font font = new Font(base.AnalogMeter.Font.FontFamily, 6f * drawRatio);
                float num17 = x + ((float) ((num14 - (20f * drawRatio)) * Math.Cos((double) num13)));
                float num18 = y + ((float) ((num14 - (20f * drawRatio)) * Math.Sin((double) num13)));
                double num19 = Math.Round((double) num15);
                string text = string.Format("{0,0:D}", (int) num19);
                SizeF ef = Gr.MeasureString(text, font);
                Gr.DrawString(text, font, brush, (float) (num17 - ((float) (ef.Width * 0.5))), (float) (num18 - ((float) (ef.Height * 0.5))));
                num15 += (float) ((maxValue - minValue) / ((double) (scaleDivisions - 1f)));
                if (i == (scaleDivisions - 1f))
                {
                    break;
                }
                if (scaleDivisions <= 0f)
                {
                    num13 += radian;
                }
                else
                {
                    for (int j = 0; j <= scaleSubDivisions; j++)
                    {
                        num13 += radian;
                        tf2.X = x + ((float) (num14 * Math.Cos((double) num13)));
                        tf2.Y = y + ((float) (num14 * Math.Sin((double) num13)));
                        tf3.X = x + ((float) ((num14 - (width / 50f)) * Math.Cos((double) num13)));
                        tf3.Y = y + ((float) ((num14 - (width / 50f)) * Math.Sin((double) num13)));
                        Gr.DrawLine(pen, tf2, tf3);
                    }
                }
                font.Dispose();
            }
            pen.Dispose();
            brush.Dispose();
            return true;
        }

        public override bool DrawGlass(Graphics Gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            if (base.AnalogMeter.ViewGlass)
            {
                Color color = Color.FromArgb(40, 200, 200, 200);
                Color color2 = Color.FromArgb(0, 200, 200, 200);
                LinearGradientBrush brush = new LinearGradientBrush(rc, color, color2, 45f);
                Gr.FillEllipse(brush, rc);
                brush.Dispose();
            }
            return true;
        }

        public override bool DrawNeedle(Graphics Gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            float width = rc.Width;
            float height = rc.Height;
            double minValue = base.AnalogMeter.MinValue;
            double maxValue = base.AnalogMeter.MaxValue;
            double num5 = base.AnalogMeter.Value;
            float startAngle = base.AnalogMeter.GetStartAngle();
            float endAngle = base.AnalogMeter.GetEndAngle();
            PointF needleCenter = base.AnalogMeter.GetNeedleCenter();
            float num8 = (width / 2f) - ((float) (width * 0.12));
            float val = (float) (maxValue - minValue);
            val = (float) ((100.0 * (num5 - minValue)) / ((double) val));
            val = ((endAngle - startAngle) * val) / 100f;
            val += startAngle;
            float radian = LBMath.GetRadian(val);
            float x = needleCenter.X;
            float y = needleCenter.Y;
            PointF tf2 = new PointF(0f, 0f);
            PointF tf3 = new PointF(0f, 0f);
            GraphicsPath path = new GraphicsPath();
            tf2.X = x;
            tf2.Y = y;
            radian = LBMath.GetRadian(val + 10f);
            tf3.X = x + ((float) ((width * 0.09f) * Math.Cos((double) radian)));
            tf3.Y = y + ((float) ((width * 0.09f) * Math.Sin((double) radian)));
            path.AddLine(tf2, tf3);
            tf2 = tf3;
            radian = LBMath.GetRadian(val);
            tf3.X = x + ((float) (num8 * Math.Cos((double) radian)));
            tf3.Y = y + ((float) (num8 * Math.Sin((double) radian)));
            path.AddLine(tf2, tf3);
            tf2 = tf3;
            radian = LBMath.GetRadian(val - 10f);
            tf3.X = x + ((float) ((width * 0.09f) * Math.Cos((double) radian)));
            tf3.Y = y + ((float) ((width * 0.09f) * Math.Sin((double) radian)));
            path.AddLine(tf2, tf3);
            path.CloseFigure();
            SolidBrush brush = new SolidBrush(base.AnalogMeter.NeedleColor);
            Pen pen = new Pen(base.AnalogMeter.NeedleColor);
            Gr.DrawPath(pen, path);
            Gr.FillPath(brush, path);
            path.Dispose();
            brush.Dispose();
            pen.Dispose();
            return true;
        }

        public override bool DrawNeedleCover(Graphics Gr, RectangleF rc)
        {
            if (base.AnalogMeter == null)
            {
                return false;
            }
            Color needleColor = base.AnalogMeter.NeedleColor;
            RectangleF rect = rc;
            float drawRatio = base.AnalogMeter.GetDrawRatio();
            Color color = Color.FromArgb(70, needleColor);
            rect.Inflate(5f * drawRatio, 5f * drawRatio);
            SolidBrush brush = new SolidBrush(color);
            Gr.FillEllipse(brush, rect);
            color = needleColor;
            Color color3 = LBColorManager.StepColor(needleColor, 0x4b);
            LinearGradientBrush brush2 = new LinearGradientBrush(rc, color, color3, 45f);
            Gr.FillEllipse(brush2, rc);
            brush2.Dispose();
            brush.Dispose();
            return true;
        }

        public override bool DrawThresholds(Graphics gr, RectangleF rc)
        {
            return false;
        }

        public override bool DrawUM(Graphics gr, RectangleF rc)
        {
            return false;
        }

        public override bool DrawValue(Graphics gr, RectangleF rc)
        {
            return false;
        }
    }
}

